//
//  MAVipView.m
//  MateAdv
//
//  Created by 茶 on 2023/9/15.
//

#import "MAVipView.h"
#import "MAVipMealView.h"

#import <MPITextKit/MPITextKit.h>

@interface MAVipView () <MPILabelDelegate>

@property (nonatomic, strong) UIView *contentView;
@property (nonatomic, strong) UIScrollView *itemScrollView;
@property (nonatomic, strong) NSMutableArray *itemArray;

@end

@implementation MAVipView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self initInterface];
        _selectIndex = 0;
    }
    return self;
}

- (void)initInterface {
    UIScrollView *scrollView = [[UIScrollView alloc] initWithFrame:self.frame];
    scrollView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    scrollView.contentSize = kScreenSize;
    [self addSubview:scrollView];
    [scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    _contentView = [UIView new];
    [scrollView addSubview:_contentView];
    [_contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    
    UIImageView *backView = [UIImageView initWithName:@"Vip_Back"];
    [_contentView addSubview:backView];
    [backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.mas_equalTo(0);
        make.width.mas_equalTo(SCREEN_WIDTH);
    }];
    
    [UILabel ma_labelWithSuperView:_contentView text:@"VIP解锁" textFont:MAFontScaleSemibold(40) textColor:UIColor.whiteColor textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(26);
        make.top.ma_offset(108);
    }];
    
    [UILabel ma_labelWithSuperView:_contentView text:@"飞行棋高阶版本" textFont:MAFontScaleSemibold(30) textColor:UIColorWhite textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(26);
        make.top.ma_offset(182);
    }];
    
    [UILabel ma_labelWithSuperView:_contentView text:@"真心话、大冒险" textFont:MAFontScaleSemibold(30) textColor:UIColorWhite textAlignment:0 makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(26);
        make.top.ma_offset(226);
    }];
    
    [UIImageView ma_imgViewWithSuperView:_contentView imgName:@"Vip_Logo" makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.top.ma_offset(134);
        make.right.ma_offset(-18);
    }];
    
    [UIImageView ma_imgViewWithSuperView:_contentView imgName:@"Vip_Heart" makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.top.ma_offset(274);
        make.left.ma_offset(19);
        make.right.ma_offset(-19);
    }];
    
    _itemScrollView = [[UIScrollView alloc] qmui_initWithSize:CGSizeMake(SCREEN_WIDTH, 135)];
    _itemScrollView.showsHorizontalScrollIndicator = NO;
    _itemScrollView.showsVerticalScrollIndicator = NO;
    [_contentView addSubview:_itemScrollView];
    [_itemScrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.ma_offset(331);
        make.height.ma_offset(150);
        make.left.right.mas_equalTo(0);
    }];
    
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.layer.cornerRadius = MARatioWidth(24);
    button.clipsToBounds = YES;
    [button setBackgroundColor:MAColor(@"#530027")];
    [button setTitleColor:UIColorWhite forState:UIControlStateNormal];
    [button setTitle:@"确定" forState:UIControlStateNormal];
    [button addTarget:self action:@selector(confirmEvent) forControlEvents:UIControlEventTouchUpInside];
    [_contentView addSubview:button];
    [button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.ma_offset(48);
        make.top.ma_offset(501);
        make.left.ma_offset(18);
        make.right.ma_offset(-18);
    }];
 
    NSString *content = @"用户协议 | 隐私政策 | 自动续费协议 | 会员服务协议";
    NSMutableAttributedString *atrString = [[NSMutableAttributedString alloc] initWithString:content attributes:@{NSFontAttributeName: MAFontScaleRegular(14)}];
    MPITextLink *privacy = [MPITextLink linkWithValue:[NSURL URLWithString:@"http://changxiu-oss.xtboo.com/zxh/web/policy/privacy.html"]];
    MPITextLink *agreement = [MPITextLink linkWithValue:[NSURL URLWithString:@"http://changxiu-oss.xtboo.com/zxh/web/policy/user_agreement.html"]];
    MPITextLink *autoAgreement = [MPITextLink linkWithValue:[NSURL URLWithString:@"http://changxiu-oss.xtboo.com/zxh/web/policy/auth.html"]];
    MPITextLink *vipAgreement = [MPITextLink linkWithValue:[NSURL URLWithString:@"http://changxiu-oss.xtboo.com/zxh/web/policy/vip_agreement.html"]];
    
    NSRange privacyRange = [content rangeOfString:@"隐私政策"];
    NSRange agreementRange = [content rangeOfString:@"用户协议"];
    NSRange autoRange = [content rangeOfString:@"自动续费协议"];
    NSRange vipRange = [content rangeOfString:@"会员服务协议"];
    
    [atrString addAttribute:MPITextLinkAttributeName value:privacy range:privacyRange];
    [atrString addAttribute:MPITextLinkAttributeName value:agreement range:agreementRange];
    [atrString addAttribute:MPITextLinkAttributeName value:autoAgreement range:autoRange];
    [atrString addAttribute:MPITextLinkAttributeName value:vipAgreement range:vipRange];
    
    MPILabel *contentLabel = [MPILabel new];
    contentLabel.delegate = self;
    contentLabel.attributedText = atrString;
    contentLabel.textAlignment = NSTextAlignmentCenter;
    contentLabel.numberOfLines = 0;
    contentLabel.textColor = UIColorWhite;
    [_contentView addSubview:contentLabel];
    [contentLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_offset(0);
        make.top.mas_equalTo(button.mas_bottom).ma_offset(40);
    }];
    
    UILabel *label = [UILabel ma_labelWithSuperView:_contentView text:@"自费服务声明 \n付款:用户确认购买并付款后计入iTunes账户;\n取消续订:如需取消续订，请在当前续订周期到期前24小时以前，手动在iTunes/AppleID设置管理中关闭自动续费功能，到期前24小时内取消，将会收取订阅费用; \n续费:苹果iTues账户会在到期前24小时内扣费，扣费成功后订阅周期顺延一个订阅周期。" textFont:MAFontScaleLight(12) textColor:UIColorWhite textAlignment:NSTextAlignmentLeft makeMasonryBlock:^(MASConstraintMaker * _Nonnull make) {
        make.left.ma_offset(20);
        make.right.mas_offset(-18);
        make.top.mas_equalTo(button.mas_bottom).ma_offset(86);
        make.bottom.ma_offset(-16);
    }];
    label.numberOfLines = 0;
    
}

- (void)confirmEvent {
    MABlock(self.confirmBlock, self.selectIndex);
}

- (void)selectItemEvent:(NSUInteger)tag {
    for (MAVipMealView *view in _itemArray) {
        view.selected = NO;
    }
    MAVipMealView *view = _itemArray[tag];
    view.selected = YES;
    _selectIndex = tag;
}

- (void)setItems:(NSArray *)items {
    _items = items;
    
    UIView *contentView = [UIView new];
    [_itemScrollView addSubview:contentView];
    [contentView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(0);
    }];
    MAWeakify(self);
    _itemArray = [NSMutableArray arrayWithCapacity:items.count];
    for (int i = 0; i < items.count; i++) {
        MAVipMealView *view = [MAVipMealView new];
        view.model = items[i];
        view.tag = i;
        if (i == 0) {
            view.selected = YES;
        } else {
            view.selected = NO;
        }
        view.selectBlock = ^(NSUInteger tag) {
            MAStrongify(self);
            [self selectItemEvent:tag];
        };
        [contentView addSubview:view];
        [_itemArray addObject:view];
        [view mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.ma_offset(16 + (107 + 10) * i);
            make.height.ma_offset(135);
            make.width.ma_offset(107);
            make.bottom.ma_offset(-4);
            make.top.ma_offset(11);
            if (i == items.count - 1) {
                make.right.ma_offset(-16);
            }
        }];
    }
}

- (void)label:(MPILabel *)label didInteractWithLink:(MPITextLink *)link forAttributedText:(NSAttributedString *)attributedText inRange:(NSRange)characterRange interaction:(MPITextItemInteraction)interaction {
    if ([link.value isKindOfClass:NSURL.class]) {
        [UIApplication.sharedApplication openURL:(NSURL *)link.value options:@{} completionHandler:nil];
    }
}

@end
